//
//  DYJDataBaseTool.m
//  FunWay
//
//  Created by dllo on 16/5/26.
//  Copyright © 2016年 dyj. All rights reserved.
//

#import "DYJDataBaseTool.h"
#import "DYJCollectionReadModel.h"
#import "XZHHomeModel.h"
#import "XZHMovieDetailModel.h"
#import "XZHHomeNoteModel.h"

@implementation DYJDataBaseTool
+ (DYJDataBaseTool *)shareDYJDataBaseTool{
    
    static DYJDataBaseTool *dataBaseTool = nil;
    static dispatch_once_t oneToKen;
    dispatch_once(&oneToKen, ^{
        
        dataBaseTool = [[DYJDataBaseTool alloc] init];
        [dataBaseTool OpenDB];
        
        [dataBaseTool createSubjectTable];
        [dataBaseTool createHomeTable];
        [dataBaseTool createMovieTable];
        [dataBaseTool createNoteTable];
    });
    
    return dataBaseTool;
}

// ------- 打开数据库 --------//
- (void)OpenDB{
    
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath = sandBox[0];
    
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"Read.sqlite"];
    NSLog(@"%@",documentPath);
    int result = sqlite3_open([documentPath UTF8String], &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"数据库打开成功");
    }else{
        NSLog(@"数据库打开失败");
    }
}

// --------- 创建阅读收藏的表格 -------------//
- (void)createSubjectTable
{
    NSString *sqliteStr = @"create table if not exists read (title text primary key, type text, url text)";
    
    int result = sqlite3_exec(dbPoint, [sqliteStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"阅读收藏表格创建成功");
    }else{
        NSLog(@"阅读收藏表格创建失败");
    }
}
// --------- 创建首页收藏的表格 -------------//
- (void)createHomeTable
{
    NSString *sqliteStr = @"create table if not exists home (hpcontent_id text primary key, hp_content text, web_url text,hp_img_url text, praisenum integer)";
    int result = sqlite3_exec(dbPoint, [sqliteStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"首页收藏表格创建成功");
    }else{
        NSLog(@"首页收藏表格创建失败");
    }
}
// --------- 创建电影收藏表格的表格 ---------- //
- (void)createMovieTable {
    NSString *sqliteStr = @"create table if not exists movie (movie_id text primary key, title text, web_url text,detailcover text)";
    int result = sqlite3_exec(dbPoint, [sqliteStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"电影收藏表格创建成功");
    }else{
        NSLog(@"电影收藏表格创建失败");
    }
}

// --------- 创建小记收藏表格的表格 ---------- //
- (void)createNoteTable {
    NSString *sqliteStr = @"create table if not exists note (content text primary key, image text, date text, placeholderImage text)";
    int result = sqlite3_exec(dbPoint, [sqliteStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"小记收藏表格创建成功");
    }else{
        NSLog(@"电影收藏表格创建失败");
    }
}


// ------- 判断是否保存 ---------------------//
- (DYJIsSaveDataInTable)isSaveDataInTableWithTitle:(NSString *)title
{
    
   // NSString *sqliteStr = [NSString stringWithFormat:@"select * from subject where model = '%@'",model];
    NSString *sqlStr = [NSString stringWithFormat:@"select * from read where title = '%@'",title];
    // 跟随指针
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"查询表格成功");
        
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            sqlite3_finalize(stmt);
            NSLog(@"已在表中");
            return DYJInTabel;
        }
        sqlite3_finalize(stmt);
        NSLog(@"没有在表中");
        return DYJNotInTable;
    }else{
        sqlite3_finalize(stmt);
        NSLog(@"查询表格失败!");
        return DYJSelectError;
    }
}

// ------------- 添加 收藏 ---------------//
- (void)insertDataWithReadTitle:(NSString *)title Type:(NSString *)type Url:(NSString *)url
{
    NSString *sqliteStr =[NSString stringWithFormat:@"insert into read (title, type, url) values('%@','%@', '%@')",title, type, url];
    
    int result = sqlite3_exec(dbPoint, [sqliteStr UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"已收藏"
                                                            message:nil
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        [alertView show];

        NSLog(@"添加成功");
    }else{
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"收藏失败"
                                                            message:nil
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        [alertView show];
        NSLog(@"添加失败: %d", result);
    }
}

- (void)insertDataWithHomeModel:(XZHHomeModel *)model
{
    NSString *sqliteStr =[NSString stringWithFormat:@"insert into home (hpcontent_id, hp_content, web_url ,hp_img_url, praisenum) values('%@', '%@', '%@', '%@', '%ld')", model.hpcontent_id, model.hp_content, model.web_url, model.hp_img_url, model.praisenum];
    int result = sqlite3_exec(dbPoint, [sqliteStr UTF8String], nil, nil, nil);

    if (result == SQLITE_OK) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"已收藏"
                                                            message:nil
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        [alertView show];
        
        NSLog(@"添加成功");
    }else{
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"收藏失败"
                                                            message:nil
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        [alertView show];
        NSLog(@"添加失败: %d", result);
    }
}

- (void)insertDataWithMovieModel:(XZHMovieDetailModel *)model {
    NSString *sqliteStr =[NSString stringWithFormat:@"insert into movie (movie_id, title, web_url, detailcover) values('%@', '%@', '%@', '%@')", model.movie_id, model.title, model.web_url, model.detailcover];
    int result = sqlite3_exec(dbPoint, [sqliteStr UTF8String], nil, nil, nil);
    NSLog(@"%@",model);
    if (result == SQLITE_OK) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"已收藏"
                                                            message:nil
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        [alertView show];
        
        NSLog(@"添加成功");
    }else{
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"收藏失败"
                                                            message:nil
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        [alertView show];
        NSLog(@"添加失败: %d", result);
    }

}

-(void)insertDataWithNoteContent:(NSString *)content Image:(NSString *)image Data:(NSString *)date PlaceholderImage:(NSString *)placeholderImage
{
    NSString *sqliteStr =[NSString stringWithFormat:@"insert into note (content, image, date, placeholderImage) values('%@', '%@', '%@', '%@')", content, image, date, placeholderImage];
    int result = sqlite3_exec(dbPoint, [sqliteStr UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"已收藏"
                                                            message:nil
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        [alertView show];
        
        NSLog(@"添加成功");
    }else{
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"收藏失败"
                                                            message:nil
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        [alertView show];
        NSLog(@"添加失败: %d", result);
    }

}


// ------------------  删除 --------------------//
- (void)delegateDataWithTitle:(NSString *)title
{
    
    NSString *sqlStr = [NSString stringWithFormat:@"delete from read where title = '%@'",title];
    
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功!");
    }else{
        NSLog(@"删除失败!");
    }
    
}

- (void)delegateDataHomeId:(NSString *)hpcontent_id
{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from home where hpcontent_id = '%@'", hpcontent_id];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功!");
    }else{
        NSLog(@"删除失败!");
    }

}

- (void)delegateDataMovieId:(NSString *)movie_id
{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from movie where movie_id = '%@'", movie_id];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功!");
    }else{
        NSLog(@"删除失败!");
    }
}

// -------------- 遍历所有取出 -------------------//
- (NSMutableArray *)selectAll{
    
    NSString *sqlStr = @"select * from read";
    
    NSMutableArray *readArr = [NSMutableArray array];
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"查询收藏成功!");
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 0);
            const unsigned char *type = sqlite3_column_text(stmt, 1);
            const unsigned char *url = sqlite3_column_text(stmt, 2);
            NSString *Title = [NSString stringWithUTF8String:(const char *)title];
            NSString *Type = [NSString stringWithUTF8String:(const char *)type];
            NSString *Url = [NSString stringWithUTF8String:(const char *)url];
            DYJCollectionReadModel *model = [[DYJCollectionReadModel alloc] init];
           
            model.title = Title;
            model.type = Type;
            model.url = Url;
            [readArr addObject:model];
        }
        
    }else{
        sqlite3_finalize(stmt);
        NSLog(@"查询失败");
    }
    sqlite3_finalize(stmt);
    return readArr;
}


- (NSMutableArray *)selectAllHp {
    NSString *sqlStr = @"select * from home";
    
    NSMutableArray *dataSource = [NSMutableArray array];
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"查询收藏所有表格成功!");
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *hpcontent_id = sqlite3_column_text(stmt, 0);
            const unsigned char *hp_content = sqlite3_column_text(stmt, 1);
            const unsigned char *web_url = sqlite3_column_text(stmt, 2);
            const unsigned char *hp_img_url = sqlite3_column_text(stmt, 3);
            NSInteger Praisenum = sqlite3_column_int64(stmt, 4);
            
            NSString *Hpcontent_id = [NSString stringWithUTF8String:(const char *)hpcontent_id];
            NSString *Hp_content = [NSString stringWithUTF8String:(const char *)hp_content];
            NSString *Web_url = [NSString stringWithUTF8String:(const char *)web_url];
            NSString *Hp_img_url = [NSString stringWithUTF8String:(const char *)hp_img_url];
            
            XZHHomeModel *model = [[XZHHomeModel alloc] init];
            model.hpcontent_id = Hpcontent_id;
            model.hp_content = Hp_content;
            model.web_url = Web_url;
            model.hp_img_url = Hp_img_url;
            model.praisenum = Praisenum;
            [dataSource addObject:model];
        }
    }else{
        sqlite3_finalize(stmt);
        NSLog(@"查询收藏失败");
    }
    sqlite3_finalize(stmt);
    return dataSource;
}

- (NSMutableArray *)selectAllMovie {

    NSString *sqlStr = @"select * from movie";
    
    NSMutableArray *dataSource = [NSMutableArray array];
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"查询收藏所有表格成功!");
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *movie_id = sqlite3_column_text(stmt, 0);
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            const unsigned char *web_url = sqlite3_column_text(stmt, 2);
            const unsigned char *detailcover = sqlite3_column_text(stmt, 3);
            
            NSString *Movie_id = [NSString stringWithUTF8String:(const char *)movie_id];
            NSString *Title = [NSString stringWithUTF8String:(const char *)title];
            NSString *Web_url = [NSString stringWithUTF8String:(const char *)web_url];
            NSString *Detailcover = [NSString stringWithUTF8String:(const char *)detailcover];
            
            XZHMovieDetailModel *model = [[XZHMovieDetailModel alloc] init];
            model.movie_id = Movie_id;
            model.title = Title;
            model.web_url = Web_url;
            model.detailcover = Detailcover;
            [dataSource addObject:model];
        }
    }else{
        sqlite3_finalize(stmt);
        NSLog(@"查询收藏失败");
    }
    sqlite3_finalize(stmt);
    return dataSource;
}


- (NSMutableArray *)selectAllNote {
    NSString *sqlStr = @"select * from note";
    
    NSMutableArray *dataSource = [NSMutableArray array];
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"查询收藏所有表格成功!");
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *content = sqlite3_column_text(stmt, 0);
            const unsigned char *image = sqlite3_column_text(stmt, 1);
            const unsigned char *date = sqlite3_column_text(stmt, 2);
            const unsigned char *placeholderImage = sqlite3_column_text(stmt, 3);
            
            NSString *Content = [NSString stringWithUTF8String:(const char *)content];
            NSString *Image = [NSString stringWithUTF8String:(const char *)image];
            NSString *Date = [NSString stringWithUTF8String:(const char *)date];
            NSString *PlaceholderImage = [NSString stringWithUTF8String:(const char *)placeholderImage];
            
            XZHHomeNoteModel *model = [[XZHHomeNoteModel alloc] init];
            model.content = Content;
            model.image = Image;
            model.date = Date;
            model.placeholderImage = PlaceholderImage;
            [dataSource addObject:model];
        }
    }else{
        sqlite3_finalize(stmt);
        NSLog(@"查询收藏失败");
    }
    sqlite3_finalize(stmt);
    return dataSource;
}


- (void)deleteAllCourse
{
    NSString *sqlStr = [NSString stringWithFormat:@"delete  from course"];
    
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除全部成功!");
    }else{
        NSLog(@"删除全部失败!");
    }
}

@end
